package pers.qianyu.month_202012.date_20201207;

/**
 * 861. 翻转矩阵后的得分
 * https://leetcode-cn.com/problems/score-after-flipping-matrix/
 *
 * @author mizzle rain
 * @date 2020-12-07 14:04
 */
public class MatrixScore2 {
    /**
     * 位运算 + 2进制
     */
    public int matrixScore(int[][] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        int height = A.length;
        int width = A[0].length;
        int res = (1 << (width - 1)) * height;

        for (int i = 1; i < width; i++) {
            int count1 = 0;
            for (int k = 0; k < height; k++) {
                if (A[k][0] == 1) {
                    count1 += A[k][i];
                } else {
                    count1 += 1 - A[k][i];
                }
            }
            int n = Math.max(count1, height - count1);
            res += (1 << (width - i - 1)) * n;
        }
        return res;
    }

    public static void main(String[] args) {
        int[][] arr = {
                {0, 0, 1, 1},
                {1, 0, 1, 0},
                {1, 1, 0, 0}
        };
        int result = new MatrixScore2().matrixScore(arr);
        System.out.println("result = " + result);
    }
}
